package com.sf.osms.dep.processor.util;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sf.osms.dep.cache.SysConfigCacheHelper;
import com.sf.osms.dep.webservice.bsp.AckbillDto;
import com.sf.osms.dep.webservice.bsp.IAckbillService;
import com.sf.osms.dep.webservice.email.EmailSender;
import com.sf.osms.dep.webservice.email.IService;
import com.sf.osms.dep.webservice.fvp.BarRecordBaseDto;
import com.sf.osms.dep.webservice.fvp.Exception_Exception;
import com.sf.osms.dep.webservice.fvp.ExpressBarRecordService;
import com.sf.osms.dep.webservice.fvp.ISubscriber;
import com.sf.osms.dep.webservice.wqs.Header;
import com.sf.osms.dep.webservice.wqs.IWaybillInfoService;
import com.sf.osms.dep.webservice.wqs.Sbody;
import com.sf.osms.dep.webservice.wqs.Shead;
import com.sf.osms.dep.webservice.wqs.WaybillAll;

public class WebserviceUtil {
	private static final String APPLICATION_NAME = "systerm_name";
	private static boolean NEED_BERFOR_DATA = true;
	private static String DEP_FVP_REGISTER_URL = "http://10.202.33.66:10086/fvp/wsservice/wsRun/subscriberService?wsdl";
	private static String DEP_FVP_SYSTEM_NAME = "ASURA_OSMS_DEP";
	private static int DEP_FVP_DATA_REQUIRE = 111;
	private static String DEP_INVOKE_EMAIL_URL = "http://10.2.200.3/SendEmailWebService/Service.svc?wsdl";
	private static String DEP_INVOKE_EMAIL_SYSTEM_NAME = "OSMS-DEP";
	private static String DEP_INVOKE_EMAIL_SENDER = "osms_support@sf-express.com|OSMS Support Team";
	private static String DEP_INVOKE_EMAIL_CLIENT_IP = "10.118.58.59";
	private static Logger logger = LoggerFactory.getLogger(WebserviceUtil.class);
	
	
	public static boolean invokeFvp(List<BarRecordBaseDto> list) throws Exception_Exception{
		String fvpAddress = SysConfigCacheHelper.getSysConfigValue("fvp_address");
		if(StringUtils.isBlank(fvpAddress)){
			fvpAddress = RouterConstants.FVP_ADDRESS;
		}
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress(fvpAddress.toString());
		factory.setServiceClass(ExpressBarRecordService.class);
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.getInInterceptors().add(new LoggingInInterceptor());
		ExpressBarRecordService fvpService = factory.create(ExpressBarRecordService.class);
		return  fvpService.batchAddExpressBarRecordMeth(list, SytermConfigUtil.getConfigValue(APPLICATION_NAME));
	}
	
	public static List<WaybillAll> invokeWQS(Header hearder,Shead sHead,Sbody sBody){
		String wqsAddress= SysConfigCacheHelper.getSysConfigValue("dep.wqs.address");
		if(StringUtils.isBlank(wqsAddress)){
			wqsAddress = RouterConstants.WQS_ADDRESS;
		}
		wqsAddress = wqsAddress.trim();
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress(wqsAddress);
		factory.setServiceClass(IWaybillInfoService.class);
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.getInInterceptors().add(new LoggingInInterceptor());
		IWaybillInfoService wqsService = factory.create(IWaybillInfoService.class);
		return  wqsService.queryBillInfoAll(hearder, sHead, sBody);
		
	}
	
	public static String invokeBSP(AckbillDto ackBillDto){
		String bspAddress = SysConfigCacheHelper.getSysConfigValue("dep.bsp.address");
		if(StringUtils.isBlank(bspAddress)){
			bspAddress = RouterConstants.BSP_DEP_ADDRESS;
		}
		bspAddress = bspAddress.trim();
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress(bspAddress);
		factory.setServiceClass(IAckbillService.class);
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.getInInterceptors().add(new LoggingInInterceptor());
		IAckbillService bspService = factory.create(IAckbillService.class);
		return  bspService.saveComAckbill(ackBillDto);
		
	}
	
	
	public static void invokeEmail(EmailSender emailSender){
		// 获取邮件配置信息
		try {
			String emailUrl = SwitchUtils.getNotNullValue(SysConfigCacheHelper.getSysConfigValue("email_url"), DEP_INVOKE_EMAIL_URL);
			String clientIp = SwitchUtils.getNotNullValue(SysConfigCacheHelper.getSysConfigValue("client_ip"),DEP_INVOKE_EMAIL_CLIENT_IP);
			String sender = SwitchUtils.getNotNullValue(SysConfigCacheHelper.getSysConfigValue("sender"),DEP_INVOKE_EMAIL_SENDER);
			String systemName = SwitchUtils.getNotNullValue(SysConfigCacheHelper.getSysConfigValue("dep.invoke.email.system.name"),DEP_INVOKE_EMAIL_SYSTEM_NAME);
			
			IService emailService = buildWsdlObject(IService.class, emailUrl);
			String result = emailService.sendEmailOSMS(systemName, clientIp, sender, emailSender.getDefReceive(), 
									  emailSender.getDefReceiveCc(), emailSender.getDefReceiveBcc(),
									  emailSender.getSubject(),emailSender.getContent());
			logger.info("邮件发送信息:" + emailSender.toString());
			logger.info("------------->发送邮箱返回结果："+result);
		} catch (Exception e) {
			logger.error("邮件发送异常" + ExceptionUtils.getExceptionStackString(e));
		}
		
	}
	
	public static <T> T buildWsdlObject(Class<T> clazz,String wsdlAddress){
		if(null == clazz || StringUtils.isBlank(wsdlAddress)){
			throw new IllegalArgumentException();
		}
		
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress(wsdlAddress.trim());
		factory.setServiceClass(clazz);
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.getInInterceptors().add(new LoggingInInterceptor());
		return factory.create(clazz);
	}
	
	public static boolean registerRouterOnKafka(List<String> sfwaybills){
		String url = SysConfigCacheHelper.getSysConfigValue("dep.fvp.route.register.url");
		String fvpDepSytemName = SysConfigCacheHelper.getSysConfigValue("dep.fvp.system.name");
		String dataRequire = SysConfigCacheHelper.getSysConfigValue("dep.fvp.data.require");
		int dr = DEP_FVP_DATA_REQUIRE;
		try {
			if(StringUtils.isNotBlank(dataRequire)){
				dr = Integer.valueOf(dataRequire);
			}
		} catch (Exception e) {
			logger.error(ExceptionUtils.getExceptionStackString(e));
			dr = DEP_FVP_DATA_REQUIRE;
		}
		
		String needBeforeData = SysConfigCacheHelper.getSysConfigValue("dep.fvp.before.data");
		boolean bd = NEED_BERFOR_DATA;
		try {
			if(StringUtils.isNotBlank(needBeforeData)){
				bd = Boolean.valueOf(needBeforeData);
			}
			
		} catch (Exception e) {
			logger.error(ExceptionUtils.getExceptionStackString(e));
			bd = NEED_BERFOR_DATA;
		}
		
		fvpDepSytemName = StringUtils.isEmpty(fvpDepSytemName) ? DEP_FVP_SYSTEM_NAME : fvpDepSytemName;
		url = StringUtils.isEmpty(url) ? DEP_FVP_REGISTER_URL : url;
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress(url);
		factory.setServiceClass(ISubscriber.class);
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.getInInterceptors().add(new LoggingInInterceptor());
		ISubscriber subscriber = factory.create(ISubscriber.class);
		String batchSubscribe = subscriber.batchSubscribe(sfwaybills,fvpDepSytemName, dr, bd);
		if(null != batchSubscribe){
			logger.error("******subscribe fvp router occour exception:" + batchSubscribe);
			return false;
		}else{
			return true;
		}
	}
	
	public static void main(String[] args) {
		EmailSender es = new EmailSender();
		es.setContent("testwest");
		es.setDefReceive("sfhq1405@sf-express.com");
		es.setSubject("test");
		invokeEmail(es);
	}
}
